/**
 * @ProjectName: p2p_core_sina 
 * @Copyright: Copyright (c) 2016, qudoulicai All Rights Reserved. 
 * @address: Hangzhou City, Zhejiang Province, China
 * @date: 2016年1月18日 下午1:43:20
 * @Description: 微信菜单
 */
package com.qdlc.p2p.dal.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.dao.WeChatMenuDao;
import com.qdlc.p2p.dal.dto.WeChatMenu;

/**
 * <p></p>
 * @author Kaiju Zhao 2016年1月18日 下午1:43:20
 * @version V1.0   
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify by user: {修改人} 2016年1月18日 下午1:43:20
 * @modify by reason:{原因}
 */
@Repository("weChatMenuDao")
public class WeChatMenuDaoImpl extends BaseDaoImpl<WeChatMenu> implements WeChatMenuDao {

    /* (non-Javadoc)
     * @see com.rd.p2p.core.cooperation.dao.WeChatMenuDao#listByIsDelete(boolean)
     */
    @Override
    public List<WeChatMenu> listByIsDelete(boolean isDelete) {
        QueryParam param = QueryParam.getInstance();
        param.addParam("isDelete", isDelete);
        param.addOrder(OrderType.ASC, "sort");
        return this.findByCriteria(param);
    }

    /* (non-Javadoc)
     * @see com.rd.p2p.core.cooperation.dao.WeChatMenuDao#listByParentId(long)
     */
    @Override
    public List<WeChatMenu> listByParentId(long parentId) {
        QueryParam param = QueryParam.getInstance();
        param.addParam("parentId", parentId);
        param.addOrder(OrderType.ASC, "sort");
        return this.findByCriteria(param);
    }

    /* (non-Javadoc)
     * @see com.rd.p2p.core.cooperation.dao.WeChatMenuDao#deleteByParentId(long)
     */
    @Override
    public void deleteByParentId(long parentId) {
        StringBuilder sql = new StringBuilder();
        sql.append(" update ").append(Global.DB_PREFIX).append("wechat_menu ");
        sql.append(" set is_delete=1 where parent_id=").append(parentId);
        Query query = em.createNativeQuery(sql.toString());
        query.executeUpdate();
    }

    /* (non-Javadoc)
     * @see com.rd.p2p.core.cooperation.dao.WeChatMenuDao#listByParentIdAndIsDelete(long, boolean)
     */
    @Override
    public List<WeChatMenu> listByParentIdAndIsDelete(long parentId, boolean isDelete) {
        QueryParam param = QueryParam.getInstance();
        param.addParam("parentId", parentId);
        param.addParam("isDelete", isDelete);
        param.addOrder(OrderType.ASC, "sort");
        return this.findByCriteria(param);
    }

    /* (non-Javadoc)
     * @see com.rd.p2p.core.cooperation.dao.WeChatMenuDao#deleteById(long)
     */
    @Override
    public void deleteById(long id) {
        StringBuilder sql = new StringBuilder();
        sql.append(" update ").append(Global.DB_PREFIX).append("wechat_menu ");
        sql.append(" set is_delete=1 where id=").append(id);
        Query query = em.createNativeQuery(sql.toString());
        query.executeUpdate();
    }

    /* (non-Javadoc)
     * @see com.rd.p2p.core.cooperation.dao.WeChatMenuDao#listByLevelAndIsDelete(byte, boolean)
     */
    @Override
    public List<WeChatMenu> listByLevelAndIsDelete(byte level, boolean isDelete) {
        QueryParam param = QueryParam.getInstance();
        param.addParam("level", level);
        param.addParam("isDelete", isDelete);
        param.addOrder(OrderType.ASC, "sort");
        return this.findByCriteria(param);
    }
    
    /* (non-Javadoc)
     * @see com.rd.p2p.core.cooperation.dao.WeChatMenuDao#listModelsByButtonType(byte)
     */
    @Override
    public List<WeChatMenu> listModelsByButtonType(byte buttonType) {
        QueryParam param = QueryParam.getInstance();
        param.addParam("buttonType", buttonType);
        param.addParam("isDelete", false);
        param.addOrder(OrderType.ASC, "sort");
        return this.findByCriteria(param);
    }
}
